import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import '../bean/BaseBean.dart';
import '../global/GlobalUrl.dart';
import '../util/ToastUtils.dart';

import 'view/itemOrderView.dart';
import '../util/HttpUtil.dart';

/**刷新来自官网的demo
 * flutter_easyrefresh   刷新的第三方组件
 * https://pub.dev/packages/flutter_easyrefresh/example
 */

class MyRefreshPage extends StatefulWidget {

//  @override
//  _ExampleState createState() {
//    return _ExampleState();
//  }
  MyRefreshPage({Key key}) : super(key: key);

  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<MyRefreshPage> {
  EasyRefreshController _controller;

  // 条目总数
  int _count = 6; //20

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();

    // 设置EasyRefresh的默认样式
//    EasyRefresh.defaultHeader = ClassicalHeader(
//      enableInfiniteRefresh: false,
//      refreshText: "拉动刷新",
//      refreshReadyText:"释放刷新",
//      refreshingText: "正在刷新...",
//      refreshedText: "刷新完成",
//      refreshFailedText: "刷新失败",
//      noMoreText: "没有更多数据",
//      infoText: "更新于 %T",
//    );

    initHttpData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
//        appBar: AppBar(
//          title: Text("EasyRefresh"),
//        ),
        body: EasyRefresh.custom(
          enableControlFinishRefresh: false,
          // 是否开启控制结束刷新
          enableControlFinishLoad: true,
          // 是否开启控制结束加载
          controller: _controller,
          // EasyRefresh控制器
//          header: ClassicalHeader(),
          header: ClassicalHeader(
            enableInfiniteRefresh: false,
            refreshText: "拉动刷新",
            refreshReadyText: "释放刷新",
            refreshingText: "正在刷新...",
            refreshedText: "刷新完成",
            refreshFailedText: "刷新失败",
            noMoreText: "没有更多数据",
            showInfo:false,  // 是否显示时间
//            infoText: "更新于 %T",
          ),
//          footer: ClassicalFooter(),
          footer:  ClassicalFooter(

            loadText: "拉动加载",
            loadReadyText: "释放加载",
            loadingText: "正在加载...",
            loadedText: "加载完成",
            loadFailedText: "加载失败",
            noMoreText: "没有更多数据",
            infoText: "更新于 %T",
          ),
          onRefresh: () async {
            await Future.delayed(Duration(seconds: 2), () {
              print('onRefresh');
              setState(() {
                _count = 20;
              });
              _controller.resetLoadState();
            });
          },
          onLoad: () async {
            await Future.delayed(Duration(seconds: 2), () {
              print('onLoad');
              setState(() {
                _count += 10;
              });
              _controller.finishLoad(noMore: _count >= 40);
            });
          },
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  return ItemOrderView();
//                  return Container(
//                    width: 60.0,
//                    height: 60.0,
//                    child: Center(
//                      child: Text('$index'),
//                    ),
//                    color:
//                        index % 2 == 0 ? Colors.grey[300] : Colors.transparent,
//                  );
                },
                childCount: _count,
              ),
            ),
          ],
        ),
//        persistentFooterButtons: <Widget>[
//          FlatButton(
//              onPressed: () {
//                _controller.callRefresh();
//              },
//              child: Text("Refresh", style: TextStyle(color: Colors.black))),
//          FlatButton(
//              onPressed: () {
//                _controller.callLoad();
//              },
//              child: Text("Load more", style: TextStyle(color: Colors.black))),
//        ]
    );
  }

  initHttpData(){

    String apiUrlhttp = GlobalUrl.PERSONAL_CENTOR;

    var map = {
      "workLife": "2",
    };

    HttpUtil.post(apiUrlhttp, data: map, headers: null, success: (data) {
      var jsonMap = BaseBean.fromJson(data);
      String result = jsonMap.result;
      String message = jsonMap.message;

      if (result != null && result == "success") {
//        Navigator.pop(context,"nickName...");
        Navigator.pop(
          context,
        );
      } else {
        if (message != null) {
          ToastUtils.toast(message);
        }
      }
    }, error: (errorMsg) {
      print("个人信息....errorMsg: $errorMsg");
    });



  }

}
